package com.bing.my.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bing.common.config.Result;
import com.bing.my.entity.Goose;
import com.bing.my.entity.Member;
import com.bing.my.mapper.MemberMapper;
import com.bing.my.service.MemberService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 会员数据
 * </p>
 *
 * @author bing
 * @since 2021-10-12
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Resource
    private MemberMapper memberMapper;

    @RequiresAuthentication
    @PostMapping("/selectMember")
    public Result<?> selectMemberById(@RequestBody Member member1) {
        Member member = memberMapper.selectById(member1.getId());
        System.out.println(member);
        if (member != null) {
            return Result.success(member);
        }
        return Result.error(503,"会员信息获取失败");
    }

    /**
     * 添加新会员
     * 需要登录权限
     *
     * @param member 会员
     * @return com.bing.common.config.Result<?>
     * @author Bing
     * @date 2021/10/13 16:11
     */
    @RequiresAuthentication
    @PostMapping
    public Result<?> insert(@Validated @RequestBody Member member) {
        if (memberService.insert(member) != 1) {
            return Result.error();
        }
        return Result.success();
    }

    /**
     * 根据id删除会员
     * 需要登录权限
     *
     * @param id id
     * @return com.bing.common.config.Result<?>
     * @author Bing
     * @date 2021/10/13 16:14
     */
    @RequiresAuthentication
    @DeleteMapping("/{id}")
    public Result<?> deleteById(@PathVariable String id) {
        int deleteById = memberService.deleteById(id);
        if (deleteById != 1) {
            return Result.error(501, "会员删除失败!");
        }
        return Result.success("删除成功!");
    }

    /**
     * 修改会员
     * 需要登录权限
     *
     * @param member 会员
     * @return com.bing.common.config.Result<?>
     * @author Bing
     * @date 2021/10/13 16:15
     */
    @RequiresAuthentication
    @PutMapping
    public Result<?> update(@Validated @RequestBody Member member) {
        int update = memberService.update(member);
        if (update != 1) {
            return Result.error(501, "会员修改失败!");
        }
        return Result.success("修改成功!");
    }

    /**
     * 分页查询会员
     * 需要登录权限
     *
     * @param pageNum  页码
     * @param pageSize 显示页数
     * @param search   搜索关键字
     * @param select   搜索类型
     * @return com.bing.common.config.Result<?>
     * @author Bing
     * @date 2021/10/13 16:16
     */
    @RequiresAuthentication
    @GetMapping
    public Result<?> findPage(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam String search,
                              @RequestParam String select) {
        Page<Member> memberPage = memberService.selectPage(search, select, pageNum, pageSize);
        return Result.success(memberPage);
    }

}

